<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notepad</title>

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- bootstrapValidator -->
    <link rel="stylesheet" href="./assets/plugins/plugins/bootstrap-validator/bootstrapValidator.min.css">

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- mui -->
    <link rel="stylesheet" href="./assets/plugins/plugins/mui/css/mui.min.css">

    <!-- 重置样式 -->
    <link rel="stylesheet/less" href="./assets/css/reset.less" />

    <!-- 公共样式 -->
    <link rel="stylesheet/less" href="./assets/css/common.less" />

    <!-- 页面独立的 -->
    <link rel="stylesheet/less" href="./assets/css/UserLogin.less" />
</head>
<body>
    <!-- 用户信息 -->
    <div class="user">
        <div class="avatar wow fadeInUp">
            <img src="./assets/images/avatar.png" />
        </div>

        <!-- <div class="notice wow fadeInUp" data-wow-delay="100ms">请填写已有账号或新注册账号</div> -->
    </div>

    <form class="login wow fadeInUp" id="onRegister" data-wow-delay="200ms">
        <div class="form-group">
            <label for="mobile">手机号</label>
            <input type="mobile" class="form-control" id="mobile" name="mobile" placeholder="请输入手机号" required />
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" required />
        </div>
        
        <div class="action">
            <button class="btn">管理员注册</button>
        </div>
        <div class="action">
            <a href="./login.html" class="btn">登录</a>
        </div>
    </form>
</body>
</html>
<!-- 解析less的文件 -->
<script src="./assets/js/less.min.js"></script>
<!-- jquery  -->
<script src="./assets/js/jquery.min.js"></script>
<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<script src="./assets/plugins/plugins/bootstrap-validator/bootstrapValidator.min.js"></script>

<!-- axios -->
<script src="./assets/plugins/plugins/axios/axios.min.js"></script>
<!-- request -->
<script src="./assets/plugins/plugins/axios/request.js"></script>
<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>
<!-- mui -->
<script src="./assets/plugins/plugins/mui/js/mui.min.js"></script>
<script>
    //实例化wow.js
    new WOW().init()


    document.addEventListener('DOMContentLoaded',async function() {
            let cookie3 = document.cookie[0];
            console.log(cookie3);
            
            if (cookie3 === 'b') {
                mui.toast('您已登录无需重复登录');
                setTimeout(() => {
                    location.href = 'UserIndex.html';
                }, 2000);
                return;
            }
        });

    $('#onRegister').bootstrapValidator({
        // 填写配置项
        message:'表单内容不能为空',// 所有字段默认错误信息
        // 字段校验图标。默认bootstrap图标
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        // 需要验证的字段
        fields:{
            mobile:{
                validators: {
                    notEmpty: { // 非空校验
                        message: '手机号不能为空'
                    },
                    stringLength: { // 长度校验
                        min: 11,
                        max: 11,
                        message: '手机号必须是11位'
                    },
                    regexp: { //正则校验
                        regexp: /^1[3456789]\d{9}$/, 
                        message:'手机号错误'
                    },
                }
            },
            password:{
                validators:{
                    notEmpty: { // 非空校验
                        message: '密码不能为空'
                    },
                }
            }
        }
    }).on('success.form.bv',function(e){
        // 阻止多次提交
        e.preventDefault();
    });
    // 获取表单提交
    $('#onRegister').on('submit',async function(){
        // 如果验证不过就不执行后面代码 data => js dateset
        if(!$('#onRegister').data('bootstrapValidator').isValid())
        {
            // 重置表单
            console.log(1);
            $('#onRegister').data('bootstrapValidator').resetForm();
            return;
        }
        // 获取账号密码的值，trim去空格
        let data = {
            mobile:$.trim($('#mobile').val()),
            password:$.trim($('#password').val())
        }

        // 发起请求
        let result= await POST({
            url:'business/register',
            params:data
        });

        if(result && result.code === 1)
        {
            // 提示信息
            mui.toast(result.msg);

            // 等待两秒后再执行里面代码
            setTimeout(() => {
                location.href = 'login.html';
            },2000);

            return;
        }else{
            // 提示信息
            mui.toast(result.msg);

            // 重置表单
            $('#onRegister').data('bootstrapValidator').resetForm();
            return;
        }
    });

</script>